package com.interauto.service.web;

import com.interauto.Dao.Mapper.CaseStInter;
import com.interauto.Dao.Mapper.InterStInter;
import com.interauto.Dao.Mapper.MgCaseInfoInter;
import com.interauto.Dao.entity.Caseinfo;
import com.interauto.Dao.entity.Interinfo;
import com.interauto.bean.web.CaseinfoWeb;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 处理用例页面的逻辑相关
 */
@Component
@Service
public class CasePageService {
    private static final Logger logger= LoggerFactory.getLogger(CasePageService.class);
    @Autowired
    private CaseStInter caseStInter;
    @Autowired
    private InterStInter interStInter;
    @Autowired
    private MgCaseInfoInter mgCaseInfoInter;


    public List<CaseinfoWeb> getCaseinfoList(int projectid){
        try{
            List<Caseinfo> caseinfos=caseStInter.getForProjectid(projectid);
            if(caseinfos == null || caseinfos.isEmpty()){
                return null;
            }
            List<CaseinfoWeb> caseinfoWebs=new ArrayList<>();
            for(Caseinfo caseinfo:caseinfos){
                CaseinfoWeb caseinfoWeb=new CaseinfoWeb();
                caseinfoWeb.setCaseinfo(caseinfo.getCaseinfo());
                caseinfoWeb.setCasename(caseinfo.getCasename());
                caseinfoWeb.setCaseid(caseinfo.getId());
                caseinfoWeb.setCreatetime(caseinfo.getCreatetime());
                List<Interinfo> interinfos=interStInter.getInterinfoforCaseid(caseinfo.getId());
                caseinfoWeb.setIntercount(interinfos!=null?interinfos.size():0);  //设置该case关联的用例数量
                caseinfoWebs.add(caseinfoWeb);
            }
            return caseinfoWebs;
        }
        catch (Exception e){
            e.printStackTrace();
            logger.error("getCaseinfoList is system error--->"+e);
            return null;
        }
    }

    /**
     * 对用例进行添加
     * @param caseinfoWeb
     * @return
     */
    public int addcaseinfo(CaseinfoWeb caseinfoWeb){
        if(caseinfoWeb == null){
            return -1;
        }
        logger.info("--->>addcaseinfo is:"+caseinfoWeb.toString());
        Caseinfo caseinfo=new Caseinfo();
        caseinfo.setCasename(caseinfoWeb.getCasename());
        caseinfo.setCaseinfo(caseinfoWeb.getCaseinfo());
        caseinfo.setProjectid(caseinfoWeb.getProjectid());
        try{
            mgCaseInfoInter.addcaseinfo(caseinfo);
            return caseinfo.getId();
        }
        catch (Exception e){
            logger.error("addcaseinfo is system error;caseinfo is ---->"+caseinfo.toString());
            return -1;
        }
    }

    /**
     * 对case信息进行修改
     * @param caseinfoWeb 要修改的case内容
     */
    public void upcaseinfo(CaseinfoWeb caseinfoWeb){
        if(caseinfoWeb == null){
            return;
        }
        logger.info("--->>upcaseinfo is:"+caseinfoWeb.toString());
        Caseinfo caseinfo=new Caseinfo();
        caseinfo.setCasename(caseinfoWeb.getCasename());
        caseinfo.setCaseinfo(caseinfoWeb.getCaseinfo());
        caseinfo.setId(caseinfoWeb.getCaseid());
        try{
            mgCaseInfoInter.udcaseinfo(caseinfo);
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }


    public CaseinfoWeb getCaseinfo(int caseid){
        try{
            Caseinfo caseinfo=caseStInter.getCaseinfo(caseid);
            if(caseinfo == null){
                return null;
            }
            CaseinfoWeb caseinfoWeb=new CaseinfoWeb();
            caseinfoWeb.setCaseid(caseinfo.getId());
            caseinfoWeb.setCasename(caseinfo.getCasename());
            caseinfoWeb.setCaseinfo(caseinfo.getCaseinfo());
            caseinfoWeb.setProjectid(caseinfo.getProjectid());
            caseinfoWeb.setCreatetime(caseinfo.getCreatetime());
            return caseinfoWeb;
        }
        catch (Exception e){
            logger.error("getCaseinfo is system error --->"+e);
            return null;
        }
    }

    /**
     * 对用例进行删除
     * @param caseid 要删除的用例ID
     */
    public void delcaseinfo(int caseid){
        try{
            mgCaseInfoInter.delcaseinfo(caseid);
        }
        catch (Exception e){
            e.printStackTrace();
            logger.error("--->>>delcaseinfo is system error;"+e);
        }
    }

}
